package com.csw.android.player.view.video

import android.content.Context
import android.util.AttributeSet
import androidx.lifecycle.Observer
import com.csw.android.dev_utils.ex.ViewEx.gone
import com.csw.android.player.base.AbsMediaControllerView
import com.csw.android.player.base.IMediaPlayer
import com.csw.android.player.base.MediaInfo
import com.csw.android.player.view.controller.FloatWindowMediaControllerView
import com.csw.android.player.view.controller.FullScreenMediaControllerView

open class FloatWindowVideoView(context: Context, attrs: AttributeSet? = null) :
    VideoView(context, attrs) {

    private val playingMediaListener = Observer<MediaInfo?> {
        mediaInfo = it
    }

    /** 播放器解绑监听 */
    var onPlayerUnboundListener: (() -> Unit)? = null

    init {
        mGestureView.transformGestureEnable = false
    }

    override fun createMediaController(context: Context): AbsMediaControllerView? {
        return FloatWindowMediaControllerView(context)
    }

    override fun onPlayerBind(mediaPlayer: IMediaPlayer) {
        super.onPlayerBind(mediaPlayer)
        mediaPlayer.playList?.currMedia?.observeForever(playingMediaListener)
        if (isPlaying()) {
            mPreviewView.gone()
        }
    }

    override fun onPlayerUnbind(mediaPlayer: IMediaPlayer) {
        mediaPlayer.playList?.currMedia?.removeObserver(playingMediaListener)
        super.onPlayerUnbind(mediaPlayer)
        onPlayerUnboundListener?.invoke()
    }
}